package byx.container.annotation.demo.PublishSubscribeDemo;

import byx.container.annotation.PackageContainer;
import byx.container.core.Container;
import org.junit.jupiter.api.Test;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * 发布-订阅者模式
 */
public class PublishSubscribeDemo {
    @Test
    public void test() {
        Container container = new PackageContainer();

        MessageBus bus = container.getObject(MessageBus.class);

        Flag.set.clear();
        bus.send("sports", "sports news");
        assertEquals(Set.of("SportsSubscriber1: sports news", "SportsSubscriber2: sports news"), Flag.set);

        Flag.set.clear();
        bus.send("social", "social news");
        assertEquals(Set.of("SocialSubscriber1: social news", "SocialSubscriber2: social news", "SocialSubscriber3: social news"), Flag.set);

        Flag.set.clear();
        bus.send("local", "local news");
        assertEquals(Set.of("DefaultSubscriber: local news"), Flag.set);
    }
}
